<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评测机构" prop="evaluateInstitution">
        <el-input
          v-model="queryParams.evaluateInstitution"
          placeholder="请输入评测机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评测人员" prop="evaluatePerson">
        <el-input
          v-model="queryParams.evaluatePerson"
          placeholder="请输入评测人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保单号" prop="contractNumber">
        <el-input
          v-model="queryParams.contractNumber"
          placeholder="请输入保单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被评测机构" prop="byEvaluateInstitution">
        <el-input
          v-model="queryParams.byEvaluateInstitution"
          placeholder="请输入被评测机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被评测机构三合一码" prop="byEvaluateCode">
        <el-input
          v-model="queryParams.byEvaluateCode"
          placeholder="请输入被评测机构三合一码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['insurance:insurance_before:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['insurance:insurance_before:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['insurance:insurance_before:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['insurance:insurance_before:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="insurance_beforeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="保前评测ID" align="center" prop="evaluateBeforeId" />
      <el-table-column label="评测机构" align="center" prop="evaluateInstitution" />
      <el-table-column label="评测人员" align="center" prop="evaluatePerson" />
      <el-table-column label="保单号" align="center" prop="contractNumber" />
      <el-table-column label="被评测机构" align="center" prop="byEvaluateInstitution" />
      <el-table-column label="被评测机构三合一码" align="center" prop="byEvaluateCode" />
      <el-table-column label="评测日期" align="center" prop="evaluateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.evaluateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评测报告" align="center" prop="evaluateContract" />
      <el-table-column label="漏洞个数" align="center" prop="bugNumber" />
      <el-table-column label="整改期限" align="center" prop="rectifyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rectifyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="整改完成度" align="center" prop="rectifyCompleteness" />
      <el-table-column label="评测分数" align="center" prop="evaluateGoal" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['insurance:insurance_before:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['insurance:insurance_before:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewDetails(scope.row)"
            v-hasPermi="['insurance:insurance_before:view']"
          >详细漏洞</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改保前安全评测对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="showfirst" label="评测机构" prop="evaluateInstitution">
          <el-input v-model="form.evaluateInstitution" placeholder="请输入评测机构" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="评测人员" prop="evaluatePerson">
          <el-input v-model="form.evaluatePerson" placeholder="请输入评测人员" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="保单号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入保单号" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="被评测机构" prop="byEvaluateInstitution">
          <el-input v-model="form.byEvaluateInstitution" placeholder="请输入被评测机构" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="被评测机构三合一码" prop="byEvaluateCode">
          <el-input v-model="form.byEvaluateCode" placeholder="请输入被评测机构三合一码" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="评测日期" prop="evaluateDate">
          <el-date-picker clearable
            v-model="form.evaluateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择评测日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="showfirst" label="评测报告" prop="evaluateContract">
          <file-upload v-model="form.evaluateContract"/>
        </el-form-item>
        <el-form-item v-if="showfirst" label="漏洞个数" prop="bugNumber">
          <el-input v-model="form.bugNumber" placeholder="请输入漏洞个数" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="整改期限" prop="rectifyDate">
          <el-date-picker clearable
            v-model="form.rectifyDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择整改期限">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="showfirst" label="整改完成度" prop="rectifyCompleteness">
          <el-input v-model="form.rectifyCompleteness" placeholder="请输入整改完成度" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="评测分数" prop="evaluateGoal">
          <el-input v-model="form.evaluateGoal" placeholder="请输入评测分数" />
        </el-form-item>
        <el-divider v-if="showsecond" content-position="center">详细漏洞信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-if="showsecond" type="primary" icon="el-icon-plus" size="mini" @click="handleAddConcreteBug">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-if="showsecond"  type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteConcreteBug">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="concreteBugList" :row-class-name="rowConcreteBugIndex" @selection-change="handleConcreteBugSelectionChange" ref="concreteBug">
          <el-table-column v-if="showsecond" type="selection" width="50" align="center" />
          <el-table-column v-if="showsecond" label="序号" align="center" prop="index" width="50"/>
          <el-table-column v-if="showsecond" label="单个漏洞记录" prop="singleBug" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.singleBug" placeholder="请输入单个漏洞记录" />
            </template>
          </el-table-column>
          <el-table-column v-if="showsecond" label="整改期限" prop="rectifyDeadlineTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.rectifyDeadlineTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择整改期限" />
            </template>
          </el-table-column>
          <el-table-column v-if="showsecond" label="整改时间" prop="rectifyTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.rectifyTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择整改时间" />
            </template>
          </el-table-column>
          <el-table-column v-if="showsecond" label="是否整改" prop="isRectify" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isRectify" placeholder="请输入是否整改" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInsurance_before, getInsurance_before, delInsurance_before, addInsurance_before, updateInsurance_before } from "@/api/insurance/insurance_before";

export default {
  name: "Insurance_before",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedConcreteBug: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保前安全评测表格数据
      insurance_beforeList: [],
      // 详细漏洞表格数据
      concreteBugList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      showfirst: true, 
      showsecond: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        evaluateInstitution: null,
        evaluatePerson: null,
        contractNumber: null,
        byEvaluateInstitution: null,
        byEvaluateCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        evaluateInstitution: [
          { required: true, message: "评测机构不能为空", trigger: "blur" }
        ],
        evaluatePerson: [
          { required: true, message: "评测人员不能为空", trigger: "blur" }
        ],
        contractNumber: [
          { required: true, message: "保单号不能为空", trigger: "blur" }
        ],
        byEvaluateInstitution: [
          { required: true, message: "被评测机构不能为空", trigger: "blur" }
        ],
        evaluateDate: [
          { required: true, message: "评测日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询保前安全评测列表 */
    getList() {
      this.loading = true;
      listInsurance_before(this.queryParams).then(response => {
        this.insurance_beforeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        evaluateBeforeId: null,
        evaluateInstitution: null,
        evaluatePerson: null,
        contractNumber: null,
        byEvaluateInstitution: null,
        byEvaluateCode: null,
        evaluateDate: null,
        evaluateContract: null,
        bugNumber: null,
        rectifyDate: null,
        rectifyCompleteness: null,
        evaluateGoal: null
      };
      this.concreteBugList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.evaluateBeforeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保前安全评测";
    },
    /** 查看按钮操作 */
    handleViewDetails(row) {
      this.reset();
      const evaluateBeforeId = row.evaluateBeforeId || this.ids
      getInsurance_before(evaluateBeforeId).then(response => {
        this.form = response.data;
        this.concreteBugList = response.data.concreteBugList;
        this.open = true;
        this.showfirst=false;
        this.showsecond=true;
        this.title = "修改保前安全评测";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const evaluateBeforeId = row.evaluateBeforeId || this.ids
      getInsurance_before(evaluateBeforeId).then(response => {
        this.form = response.data;
        this.concreteBugList = response.data.concreteBugList;
        this.open = true;
        this.showfirst=true;
        this.showsecond=false;
        this.title = "修改保前安全评测";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.concreteBugList = this.concreteBugList;
          if (this.form.evaluateBeforeId != null) {
            updateInsurance_before(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInsurance_before(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const evaluateBeforeIds = row.evaluateBeforeId || this.ids;
      this.$modal.confirm('是否确认删除保前安全评测编号为"' + evaluateBeforeIds + '"的数据项？').then(function() {
        return delInsurance_before(evaluateBeforeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 详细漏洞序号 */
    rowConcreteBugIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 详细漏洞添加按钮操作 */
    handleAddConcreteBug() {
      let obj = {};
      obj.singleBug = "";
      obj.rectifyDeadlineTime = "";
      obj.rectifyTime = "";
      obj.isRectify = "";
      this.concreteBugList.push(obj);
    },
    /** 详细漏洞删除按钮操作 */
    handleDeleteConcreteBug() {
      if (this.checkedConcreteBug.length == 0) {
        this.$modal.msgError("请先选择要删除的详细漏洞数据");
      } else {
        const concreteBugList = this.concreteBugList;
        const checkedConcreteBug = this.checkedConcreteBug;
        this.concreteBugList = concreteBugList.filter(function(item) {
          return checkedConcreteBug.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleConcreteBugSelectionChange(selection) {
      this.checkedConcreteBug = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('insurance/insurance_before/export', {
        ...this.queryParams
      }, `insurance_before_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
